﻿using System;
using System.Net;
using StackExchange.Redis;

namespace PubSub.S3.Redis
{
    internal class RedisHelper
    {
        public static ConnectionMultiplexer ConnectionRedis(EndPoint endPoint, string password)
        {
            var configurationOptions = new ConfigurationOptions
            {
                EndPoints =
                {
                    endPoint
                    //{"127.0.0.1",6379}
                },
                //注释ServiceName，非哨兵模式会报错，哨兵模式已经废弃，目前只支持单节点和集群模式
                //ServiceName = endPoint.ToString(),
                AllowAdmin = true,
                //Password = password,
                SyncTimeout = (int) TimeSpan.FromSeconds(value: 60).TotalMilliseconds,
            };
            if (!string.IsNullOrWhiteSpace(password))
            {
                configurationOptions.Password = password;
            }

            var connectionMultiplexer = ConnectionMultiplexer.Connect(configurationOptions);
            connectionMultiplexer.PreserveAsyncOrder = false;
            connectionMultiplexer.ConnectionRestored += ConnectionMultiplexerOnConnectionRestored;
            connectionMultiplexer.ConnectionFailed += ConnectionMultiplexerOnConnectionFailed;
            return connectionMultiplexer;
        }

        static void ConnectionMultiplexerOnConnectionRestored(object sender,
            ConnectionFailedEventArgs connectionFailedEventArgs)
        {
        }

        static void ConnectionMultiplexerOnConnectionFailed(object sender,
            ConnectionFailedEventArgs connectionFailedEventArgs)
        {
        }
    }
}